package com.srwl.mytx.utils;

import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.RectF;
import android.media.ExifInterface;
import android.net.Uri;
import android.text.TextUtils;

import java.io.IOException;
import java.util.Random;


/**
 * Created by dahai2070 on 2018/3/23 0023.
 */

public class IMImageUtils {
    private static final String TAG = "IMImageUtils";
    private static final int[] THUMNAIL_SIZE = new int[]{91700, 367500};
    private static final float[] ROUND_RADIUS_MULTIPLE = new float[]{0.5F, 1.0F};
    public static int getOrientation(String imagePath, Context context, Uri photoUri) {
        int nOrientation ;
        ExifInterface exif;
        if(!TextUtils.isEmpty(imagePath)) {
            try {
                exif = new ExifInterface(imagePath);
                 nOrientation = exif.getAttributeInt("Orientation", 1);
                switch(nOrientation) {
                    case 3:
                        return 180;
                    case 6:
                        return 90;
                    case 8:
                        return 270;
                }
            } catch (IOException var11) {
                var11.printStackTrace();
            }
        } else if(context != null && photoUri != null) {
            Cursor cursor = null;

            int var6;
            try {
                cursor = context.getContentResolver().query(photoUri, new String[]{"orientation"}, (String)null, (String[])null, (String)null);
                if(cursor == null || cursor.getCount() != 1) {
                    byte var16 = 0;
                    return var16;
                }

                cursor.moveToFirst();
                int ret = cursor.getInt(0);
                var6 = ret;
            } catch (Throwable var12) {
                var12.printStackTrace();
                return 0;
            } finally {
                if(cursor != null) {
                    cursor.close();
                    exif = null;
                }

            }

            return var6;
        }

        return 0;
    }
   public static Bitmap getRoundBitmap(Bitmap bitmap, int width) {
        if(bitmap == null) {
            return null;
        } else {
            Bitmap output = null;

            try {
                output = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
            } catch (OutOfMemoryError var9) {
                return null;
            }

            float radius = 0.0F;
            Canvas canvas = new Canvas(output);
            canvas.drawColor(0);
            Rect dstRect = new Rect(0, 0, width, width);
            Rect srcRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
            RectF rectF = new RectF(dstRect);
            Paint paint = new Paint(1);
            canvas.drawRoundRect(rectF, radius, radius, paint);
            paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
            canvas.drawBitmap(bitmap, srcRect, dstRect, paint);
            canvas.save();
            canvas.restore();
            return output;
        }
    }

    public static Bitmap getRoundBitmap(Bitmap bitmap, int width, float radius) {
        Bitmap output = null;

        try {
            output = Bitmap.createBitmap(width, width, Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError var9) {
            return null;
        }

        Canvas canvas = new Canvas(output);
        canvas.drawColor(0);
        Rect dstRect = new Rect(0, 0, width, width);
        Rect srcRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        RectF rectF = new RectF(dstRect);
        Paint paint = new Paint(1);
        canvas.drawRoundRect(rectF, radius, radius, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, srcRect, dstRect, paint);
        canvas.save();
        canvas.restore();
        return output;
    }

    public static Bitmap getRoundBitmap(Bitmap bitmap, int width, int height, float pRadius) {
        Bitmap output = null;

        try {
            output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        } catch (OutOfMemoryError var11) {
            return null;
        }

        float radius = getBestCutRadius(width * height, pRadius);
        Canvas canvas = new Canvas(output);
        canvas.drawColor(0);
        Rect dstRect = new Rect(0, 0, width, height);
        Rect srcRect = new Rect(0, 0, bitmap.getWidth(), bitmap.getHeight());
        RectF rectF = new RectF(dstRect);
        Paint paint = new Paint(1);
        canvas.drawRoundRect(rectF, radius, radius, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, srcRect, dstRect, paint);
        canvas.save();
        canvas.restore();
        return output;
    }
    public static float getBestCutRadius(int currentSize, float currentRadius) {
        int min = Math.abs(currentSize - THUMNAIL_SIZE[0]);
        int j = 0;

        for(int i = 1; i < THUMNAIL_SIZE.length; ++i) {
            if(Math.abs(currentSize - THUMNAIL_SIZE[i]) < min) {
                min = Math.abs(currentSize - THUMNAIL_SIZE[i]);
                j = i;
            }
        }

        return ROUND_RADIUS_MULTIPLE[j] * currentRadius;
    }







    public static String genImageName() {
        //取当前时间的长整形值包含毫秒
        long millis = System.currentTimeMillis();
        //long millis = System.nanoTime();
        //加上三位随机数
        Random random = new Random();
        int end3 = random.nextInt(999);
        //如果不足三位前面补0
        String str = millis +String.format("%03d", end3);

        return str;
    }

    /**
     * 将一个bitmap转化为圆形输出
     *
     * @param bitmap
     * @return
     */
    public static Bitmap toRoundBitmap(Bitmap bitmap) {
        int width = bitmap.getWidth();
        int height = bitmap.getHeight();
        int r = 0;
        if (width < height) {
            r = width;
        } else {
            r = height;
        }
        Bitmap backgroundBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(backgroundBitmap);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        RectF rectF = new RectF(0, 0, r, r);
        canvas.drawRoundRect(rectF, r / 2, r / 2, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, null, rectF, paint);
        return backgroundBitmap;
    }
}
